#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
#
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Add_Delta_YM Add_Delta_Days Today);

use avsCommon;

our (@begdate, @enddate);
our ($dbhstats, $sqlstats, $sthstats, $dbhlogin, $sqllogin, $sthlogin);
our ($vendorid, $collegeid, $username, $password, $option1, $option2, $vendorusername, $vendorpassword);

dbsetup();

my ($begyear, $begmonth, $begday) = @begdate;
my ($endyear, $endmonth, $endday) = @enddate;

my ($content, $response, $request, $url);

# go through all logins for vendor
do {
  getStats ();
} while ($sthlogin->fetch);

dbshutdown();

sub getStats{
	my $ua = LWP::UserAgent->new();
	$ua->cookie_jar( {} );

	# post login information
	$url = "http://admin.xreferplus.com/login.do;";
	$request = new HTTP::Request POST => $url;
	$request->content_type('application/x-www-form-urlencoded');
	$request->content("username=$username&password=$password");
	$response = $ua->request($request);
	if (!$response->is_success) {
		die "#error:$0: POST login $url : " . $response->status_line;
	};

	#$content=$response->decoded_content;

	#Post report request
	$url = 'http://admin.xreferplus.com/statscust.do';
	$request = new HTTP::Request POST => $url;
	$request->content_type('application/x-www-form-urlencoded');
	$request->content("yearFrom=$begyear&monthFrom=$begmonth&dayFrom=1&yearTo=$endyear&monthTo=$endmonth&dayTo=$endday&title=Custom+Report&reportOn=searches&groupFields=year&groupFields=month&selectFields=sessions&selectFields=searches&sortBy=default&sortDir=desc&reportFmt=htm&buttonSend.x=54&buttonSend.y=14");
	$response = $ua->request($request);
	if (!$response->is_success) {
		die "#error:$0: POST report $url : " . $response->status_line;
	};

	# they display "working" screen that requires extra send to get report content.
	$url = 'http://admin.xreferplus.com/statscust.do';
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET report $url : " . $response->status_line;
	};

	$content=$response->decoded_content;
	
	# save contents for possible debugging
	savecontent('CredoReference', "$collegeid", $content);

	if ($content =~ /Sort by searches.*?\<\/tr\>(?<data>.*?)\<\/table\>/s) {

		my $data = $+{data};

		while ($data =~ /\<tr\>.*?\<td.*?\/td>.*?\<td.*?\>(?<year>\d*)\<.*?\<td.*?\>(?<month>\d*)\<.*?\<td.*?\>(?<sessions>\d*)\<.*?\<td.*?\>(?<searches>\d*)\<.*?\/tr\>/sgc) {
			my $period= myperiod($+{year},$+{month});
			print "$vendorid, $collegeid, 'Credo Reference', $period, $+{sessions}, $+{searches}\n";
			$sthstats->execute($vendorid, $collegeid, 'Credo Reference', $period, $+{sessions}, 1)
				or die '#error: sthstats execute 1 failed';
			$sthstats->execute($vendorid, $collegeid, 'Credo Reference', $period, $+{searches}, 2)
				or die '#error: sthstats execute 2 failed';
		}

  }
  else {
  	warn "#info: no data found for $collegeid\n"
  };

	#logout session
	$url = 'http://admin.xreferplus.com/logout.do';
	$response = $ua->request(GET $url);
};